package com.cn.bulu.utils.MD5Utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class MD5Utils {
    public final static String MD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};       
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void main(String[] args) {
		System.out.println(getUUID());
		
		

	}
    /**
     * 然后将所有用户请求的参数按照字母排序（包括timestamp，token），然后更具MD5加密（可以加点盐），全部大写，生成sign签名
     * @param params
     * @param encode
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String createSign(Map<String, String> params, boolean encode)  
            throws UnsupportedEncodingException {  
        Set<String> keysSet = params.keySet();  
        Object[] keys = keysSet.toArray();  
        Arrays.sort(keys);  
        StringBuffer temp = new StringBuffer();  
        boolean first = true;  
        for (Object key : keys) {  
            if (first) {  
                first = false;  
            } else {  
                temp.append("&");  
            }  
            temp.append(key).append("=");  
            Object value = params.get(key);  
            String valueString = "";  
            if (null != value) {  
                valueString = String.valueOf(value);  
            }  
            if (encode) {  
                temp.append(URLEncoder.encode(valueString, "UTF-8"));  
            } else {
                temp.append(valueString);  
            }  
        }  
      
        return MD5Utils.MD5(temp.toString()).toUpperCase();  
    }  
    /**
     * 返回uuid
     * @return
     */
    public static String getUUID(){
        String s = UUID.randomUUID().toString(); 
//        return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
        return s;
    }
    
}
